package com.app.bimo.module_accounts.mvp.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout;


import com.app.bimo.commonbusiness.util.DataUtil;
import com.app.bimo.module_accounts.R;
import com.app.bimo.module_accounts.mvp.model.entiy.PreferenceData;
import com.app.bimo.module_accounts.mvp.ui.minterface.UserPreferAdapterClickListener;

import java.util.ArrayList;
import java.util.List;


/**
 * 偏好分类
 */
public class UserPreferView extends LinearLayout {
    private GridView gridView;
    private UserPreferAdapter adapter;
    private List<PreferenceData> list = new ArrayList<>();


    private String chooseString = "";


    public UserPreferView(Context context) {
        this(context, null);
    }

    public UserPreferView(final Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(context, R.layout.account_view_user_prefer_class, this);
        gridView = findViewById(R.id.grid);
        adapter = new UserPreferAdapter(context, list);
        gridView.setAdapter(adapter);

    }

    public void setListener(UserPreferAdapterClickListener listener) {
        adapter.setListener(listener);
    }

    private String getString(List<PreferenceData> list) {
        String s = "";
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isChoose()) {
                if (DataUtil.isEmpty(s)) {
                    s = list.get(i).getCategoryid();
                } else {
                    s = s + "," + list.get(i).getCategoryid();
                }
            }
        }
        chooseString = s;
        return chooseString;
    }

    public String getChooseString() {
        return getString(list);
    }

    public int getChooseSize() {
        int num = 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isChoose()) {
                num++;
            }
        }
        return num;
    }

    public void setList(List<PreferenceData> list) {
        this.list.clear();
        this.list.addAll(list);
        adapter.notifyDataSetChanged();
    }
}
